package cn.tedu.blackhorse.service.impl;

import cn.tedu.blackhorse.ex.ServiceCode;
import cn.tedu.blackhorse.ex.ServiceException;
import cn.tedu.blackhorse.mapper.StudentProblemMapper;
import cn.tedu.blackhorse.pojo.dto.StudentProblemAddNewDTO;
import cn.tedu.blackhorse.pojo.dto.StudentProblemUpdateDTO;
import cn.tedu.blackhorse.pojo.entity.StudentProblem;
import cn.tedu.blackhorse.pojo.vo.StudentProblemListItemVO;
import cn.tedu.blackhorse.pojo.vo.StudentProblemStandardVO;
import cn.tedu.blackhorse.service.IStudentProblemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class StudentProblemServiceImpl implements IStudentProblemService {

    @Autowired
    private StudentProblemMapper studentProblemMapper;

    @Override
    public void addNew(StudentProblemAddNewDTO studentProblemAddNewDTO) {

        StudentProblem studentProblem = new StudentProblem();
        BeanUtils.copyProperties(studentProblemAddNewDTO,studentProblem);
        int row = studentProblemMapper.insert(studentProblem);
        if (row != 1){
            String message = "增添失败,系统繁忙请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void deleteById(Long id) {
        StudentProblemStandardVO standardVO = studentProblemMapper.getById(id);
        if (standardVO == null){
            String message = "删除失败,所删除的数据不存在";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int row = studentProblemMapper.deleteById(id);
        if (row != 1){
            String message = "删除失败,系统繁忙请稍后重试";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public List<StudentProblemListItemVO> list() {
        return studentProblemMapper.list();
    }

    @Override
    public void updateById(Long id, StudentProblemUpdateDTO studentProblemUpdateDTO) {

    }

    @Override
    public StudentProblemStandardVO getById(Long id) {
        StudentProblemStandardVO standardVO = studentProblemMapper.getById(id);
        if (standardVO == null){
            String message = "查询失败,查询数据不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return standardVO;
    }
}
